package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.entity.GoodsCategory;
import com.micro.modual.extension.GoodsCategoryExtension;
import com.micro.modual.mapper.GoodsCategoryMapper;
import com.micro.modual.model.bo.goodscategory.GoodsCategoryBO;
import com.micro.modual.model.bo.goodscategory.GoodsCategoryStatusBO;
import com.micro.modual.model.dto.goodscategory.GoodsCategoryListDTO;
import com.micro.modual.model.tree.CategoryTreeDTO;
import com.micro.modual.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>商品分类表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsCategoryService")
public class GoodsCategoryServiceImpl extends AbstractBasicsServiceImpl<GoodsCategory,Long> implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public RespJsonPageData<GoodsCategoryListDTO> findGoodsCategoryPageList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.findGoodsCategoryPageList(params);
        List<GoodsCategoryListDTO> goodsCategoryListDTOList = JacksonUtil.getList(JacksonUtil.toJson(goodsCategoryList),GoodsCategoryListDTO.class);
        return new RespJsonPageData<GoodsCategoryListDTO>(goodsCategoryListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增商品分类表详情</h2>
     * @param goodsCategoryBO {@link GoodsCategoryBO} 商品分类表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addGoodsCategory(GoodsCategoryBO goodsCategoryBO) {
        GoodsCategory goodsCategory = GoodsCategoryExtension.converAddGoodsCategory(goodsCategoryBO);
        goodsCategoryMapper.insertSelective(goodsCategory);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改商品分类表详情</h2>
     * @param goodsCategoryBO  {@link GoodsCategoryBO} 商品分类表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editGoodsCategory(GoodsCategoryBO goodsCategoryBO) {
        GoodsCategory goodsCategory = GoodsCategoryExtension.converEditGoodsCategory(goodsCategoryBO);
        goodsCategoryMapper.updateByPrimaryKeySelective(goodsCategory);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除商品分类表信息</h2>
     * @param id  商品分类表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteGoodsCategory(Long id) {
        GoodsCategory category = goodsCategoryMapper.selectByPrimaryKey(id);
        if(goodsCategoryMapper.hasChild(category.getCategoryCode()) > 0){
            throw new BizException(MicroResponseCode.CATEGORY_HAS_CHILD_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.CATEGORY_HAS_CHILD_NOT_ALLOW_DELETE.getResponseMessage());
        }
        goodsCategoryMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJson modifyGoodsCategoryStatus(GoodsCategoryStatusBO goodsCategoryStatusBO) {
        GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(goodsCategoryStatusBO.getCategoryId());
        if(goodsCategoryStatusBO.getCategoryStatus().equals(MicroConstant.StatusEffective.DISABLE)){
            if(goodsCategoryMapper.hasChild(goodsCategory.getCategoryCode()) > 0){
                throw new BizException(MicroResponseCode.CATEGORY_HAS_CHILD_NOT_ALLOW_CLOSE.getResponseCode(),MicroResponseCode.CATEGORY_HAS_CHILD_NOT_ALLOW_CLOSE.getResponseMessage());
            }
        }
        goodsCategory.setCategoryStatus(goodsCategoryStatusBO.getCategoryStatus());
        goodsCategoryMapper.updateByPrimaryKeySelective(goodsCategory);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJsonData<List<CategoryTreeDTO>> findCategoryTree() {
        List<GoodsCategory> categoryList = goodsCategoryMapper.selectAll();
        List<CategoryTreeDTO> categoryTreeDTOList = GoodsCategoryExtension.buildCategoryTree(categoryList);
        return new RespJsonData<List<CategoryTreeDTO>>(categoryTreeDTOList,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

}
